Boost your efficiency with refactorings, code analysis, unit test support, and an integrated debugger.

Pointers came first. They've been a built in language feature ever since the early days of C. A pointer is just a number, giving an actual location in memory[1]. When you add one to a pointer, it moves up to the next location in memory.

If you have a bunch of items that are stored adjacent to one another in memory, you can step through all of them by incrementing a pointer. So for this really simple situation, you can use a pointer as an iterator; indeed, vector iterators are usually implemented using pointers, because you can.

However, many data structures aren't stored adjacent to each other in memory. Maps and sets are implemented using binary trees, for example, and hash tables can have "gaps" between the valid elements. You can't use a pointer to iterate through these.

Instead, we write a class that looks like and acts like a pointer. You can increment it to step to the next item in a container: ++i becomes a call to the function named operator++(). You can dereference it (*i) to find out what's there: This calls a function named operator *() which returns a value of the appropriate type. Since incrementing is now a function that knows how to walk through the more complicated container type, we can use the "pointer-like" way to step through binary trees as well.

To distinguish these pseudo-pointers from the real thing, we call them "iterators," which is a description of what you are supposed to do with them. A true pointer can be used as an iterator for simple containers, but the general concept of an iterator is broader.


[1] In the bad old days, this was literally true; the pointer held the physical address of a value in memory. If you had 64,000 bytes of memory, the pointer would hold a value between 0 and 63,999.

In these more enlightened times, every program gets a virtual memory space, and pointers contain addresses in virtual memory. The operating system knows how to convert from virtual addresses to physical addresses. From the program's point of view, it has a vast, pristine memory space all to itself, and it can't modify nor even read the memory belonging to other programs, even though those programs are running on the same computer. This is much better, and makes it possible for a program to crash without bringing the rest of the computer down with it.

Iterators are a generalization of pointers. That is, they provide a pointer-like interface, but can be generalized to a much broader range of storage than real pointers.

For example, consider traversing a linked list. With raw pointers, you typically end up with a structure something on this order:

  1. template <class T> 
  2. struct node {  
  3. node *next; 
  4. T value; 
  5. }; 

...but to traverse the linked list, you need to know the name used in the structure, and the code that does the traversal needs to know that it's going to work with a linked list.

With iterators, you generalize some basic pointer operators su

A pointer can be considered as an iterator, but not always is an iterator. An iterator is a generalization of a pointer (whe it is used as iterator.)

The main advantage of using iterators is generality. That is, you can write only one function and use it for different containers:

  1. template<typename T>int sum(T begin, T end){ 
  2. int sum{0}; 
  3. while (begin != end) sum += *(begin++); 
  4. return sum; 
  5. } 

Now you can use that function for any container:

  1. std::vector<int> vi{1, 2, 3, 4}; 
  2. int vis = sum(std::begin(vi), std::end(vi)); 
  3.  
  4. int ai[] = {1, 2, 3, 4}; 
  5. int ais = sum(std::begin(ai), std::end(ai)); 
  6.  
  7. std::list li{1 

It's easier to give someone an address to your home than to give a copy of your home to everyone.

References are just like pointers, except…

  • You can update the value of a pointer, but not the value of a reference.
  • A pointer can be nullptr, but a reference must be valid.
  • A pointer can be indexed like an array, but a reference cannot be.
  • Arithmetic operators apply to the pointers directly, but apply to the target object of a reference.

C++ iterators are to pointers as ways to navigate a container as what C++ references are to pointers to objects. They are a safer and more useful alternative.

An iterator may be mutable or immutable with regards to the object it references, in other words readable, but not necessarily writable. An iterator may also be const or non-const. In any case, the syntax of the iterator is similar to that of pointers, but an iterator is a first class object and can be copied and sent as parameters, added and subtracted from, etc. It converts instantly to the object it references with the old dereference

Iterator is is an abstraction of pointer that helps you to access any container without having headache for memory address.

Let me give an example in c++

  1. map<int, int>M; 
  2. map<int, int>::iterator it; 
  3.  
  4. for(it = M.begin(); it != M.end(); it++) 
  5. { 
  6. cout<<it->first<<it-<<second<<endl; 
  7. }  

In the code above, M.begin() simply assign the initial memory address for the map container M and M.end() gives the last memory address of the map M. And what the iterator it does is, it starts accessing from the initial memory address of the map M and continues to increase the address by the size of element until it reaches

The language doesn't set a size for most types. Each implementation sets the sizes of the types it supports, and where there's leeway, what set of types it supports.

The language sets minimums, and relationships between sizes, but not the actual sizes. And before someone mentions the exact-width types, let me point out that those are all optional. So, uint32_t is exactly 32 bits, but if and only if it exists in a particular implementation.

Anyway, you asked about generic pointers.

I'm not 100% certain what you mean by generic pointer. Perhaps you mean void*, or perhaps you mean something else.

It

Yes. It will resolve to a pointer type.

Note that you can also have points towards auto like so

  1. auto* array = new int[20]; 

In this case auto will resolve to "int".

But you can also do

  1. auto array = new int[20]; 

in which case it will resolve to int*.

C++98 is the first version of ISO C++. This is a big deal. The C++ standard is an international treaty that sets out our rights and responsibilities as users of the C++ language. It allows us to write portable code, and carefully delineates the boundaries of portability. It lets us hold to account non-conforming implementations (*cough* Microsoft *cough*). It holds us to account for writing undefined behaviour. It gives us a voice in the evolution of the language, rather than allowing one single corporation to effectively dictate what the language is. On a more practical note, C++98 standardiz

An iterator is a sort of cursor. Technically, when having an array, you can go through it using a for, or a foreach (according to the language). But as containers are not arrays, you can't simply loop through them using a normal loop, unless you can access them with the operator [] (in C++ for example). Containers are "dynamics arrays" or, more precisely, objects. Then, to "loop" through these object, you use what's named an iterator.

Using this iterator, you can loop (using container.next()), loop while container.hasNext() and get the first and last items. You can even override the iterator cl

A null pointer is one which is not pointing to anything, i.e. it is assigned a null value. If there is no address to assign to a pointer, it is considered a good practice to set it to null. Every pointer type i.e int *, char * each have a null pointer value.
Syntax: <data type> *<variable name> = NULL;
Example: int *ptr = NULL;
char *ptr = '\0';

A void pointer is one which does not have any data type associated with it, i.e. it can be assigned a value of any type. Also known as the general purpose pointer, it is a C convention for a raw address. It is capable of storing address

An itorator is a specific pointer used to hold the current position in an STL data structure. Without defining an iterator, then it would not be possible to use something like the next() method on a queue or array.
The main difference is that you don't normally do much direct manipulation of the iterator, and instead let it be incremented or moved around by methods that keep it safe. With a generic pointer, it is easier to totally screw it up because you would not have safe methods for manipulating it in a template.

Iterators are an abstraction that let you work with more kinds of containers than just arrays.

As for std::set: If you have an iterator that points to a particular member of std::set, you can access the element in O(1) time, among other things. You can find the next and previous members of the set (if they exist), regardless of the key type, and without a priori knowledge of their existence.

You can't do that with an index.

No. It is the opposite. C++ is better than Java.

But it could be not. Let explain me: which is better, a sedan car or a jumbo jet?

If you want to go to a supermarket, your sedan is better. If you want to go to another country on the other side of the ocean, jumbo is better. The same thing happens, more or less, between C++ and Java. C++ is a jumbo jet and Java is a beautiful sedan. Which is better?

Therefore, for an intermediate program on a cell phone, Java is better. For a very intense CPU program, on the same cell phone, or even more difficult, on a main computer, C++ is definitely better.

Now

The distinction is sometimes just conceptual, sometimes actual. Pointers point to objects and can be dereferenced (unless its void*, also it may cause undefined behavior to dereference that points to something that isn’t a valid object).

Assuming the pointer points to an object inside an array (contiguously stored equally sized and typed objects), you can also increment it, decrement it, access it using indexing notation, compare it with another pointer that points to an object in the same array for >, <, >=, <=, ==, !=, subtract it to find distance, etc.

Random access iterators also support all

First let us try to understand the difference between Regular C and Embedded C.

There is nothing called Embedded C. There is only one C language. If you use C on computers then it is called regular C. If you use C on Electronics then it is called "Embedded C". In both the cases the compiled C program is executed by the Microprocessor only. The main difference between C on Computers and C on Electronics is Input/Output.

scanf -> read_pin - Input
printf -> write_pin. - Output

But in "C program

In C and C++, an array stores a set of elements in contiguous memory. If you create an array of size N, you have allocated storage for N elements.

A pointer doesn’t allocate anything. It only holds an address or a null value (NULL or nullptr). OK, I suppose it could also hold garbage if you fail to initialize it properly, and it can also be abused to hold certain non-pointer things non-portably. But let’s ignore those edge cases for the moment.

Where most people get confused on arrays vs. pointers is that a reference to an array decays to a pointer to its first element in certain expression cont

  1. whatever * p; 
  2.  
  3. p->value 
  4. (*p).value 

These are the same thing. One of the total disasters of C was the need to use -> or . depending on the type of the value. Today, any compiler in existence will tell you if you had written

  1. p.value 

or

  1. (*p)->value 

and tell you that you had used the wrong operator. We should have trashed the -> operator forever.

Strong disagreement with all of the C proponents here. C++17 first. Learning C first teaches you a number of bad habits and archaic practices, and, like coming from a pure Java background, stands a good chance, if you are not sufficiently flexible, of turning you into a terrible C++ programmer.

I'm a bit of a hypocrite here - I learned FORTRAN and C long before I learned C++, and the C++ I learned first was a much less mature language than the current one, especially in terms of patterns - but I would like to point out a common fallacy that appears in nearly every single response that advocates

  • Platform independency which gives Portability
    • C++ is a Platform dependent language. So, C++ programs which are compiled on one machine can only run on that particular machine on which they are compiled making it not portable.
    • Whereas Java is Platform Independent language. When java programs are compiled they are converted into some intermediate code termed as bytecode. Now, whichever machine has JVM application installed can execute already compiled source file without bothering about operating system at all making it portable.
  • Free unused Memory
    • Java has automatic garbage collection as a named th

An iterator is anything that can step through a range of objects using the ++ operator. A pointer, therefore, is an iterator. The difference is that iterators can do this for more kinds of things than a pointer and can include some other functionality (like implementing ==, +=, <, or [n] (as in array[n]).

  1. char* s = NULL; // null pointer -> 0 
  2.  
  3. void *v; // void pointer -> ?? 

A null pointer is a pointer variable that has the address

  1. 0x00000000  

in it. That is the exact definition. Informally, it also means the pointer does not “point” to a valid object of the type of pointer it is.

A void pointer is a pointer that contains an address, but is untyped. Significantly, you can not dereference a void pointer, until you cast it to a known type. All pointers are a single long integer that is interpreted as an address, so a void pointer can be a null pointer if it contains 0.

What you do with a null p

Iterator Invalidation Problem. Iterator invalidation can occur when usingiterators (usually ExternalIterators, though InternalIterators can be affected too) to traverse a mutable container. ... Any operation which changes the order of the elements in a sorted container.

If you ask this question most people will just say :

“main() in C returns void

main() in C++ return integer type value”

WRONG

On most platforms (DOS, Unix, linux, MacOS, VMS etc etc.) ALL programs have an exit code -- so main ALWAYS returns a value. Even if you don't tell it to (when you use void main() the compiler just puts in a return 0 for you -- i.e. it’s only for lazy programmers who can't be bothered to type out "return 0;")

Both C and C++ the syntax for main() should be:

int main()

or

int main(int argc, char *argv[])

It is lazy programmers who use void main() which is NOT part of the standard a

this is a keyword in C++ which is used in many contexts such as :-

  • it is used to point the current instance of a class
  • it is used to pass current object as a parameter to another method/function

Important facts-

  1. ‘this’ pointer is by default available in every instance member function of a class and it contains the address of the caller object of that class.
  2. ‘this’ pointer cannot be modified.

Very important use of ‘this’ pointer-

  1. class Box 
  2. { 
  3. private:  
  4. int l,b,h; 
  5. public: 
  6. void setDimension(int l,int b,int h) 
  7. {this->l=l; this->b=b; this->h=h;} 
  8. } 
  9. int main() 
  10. { 
  11. Box b;  
  12. b.setDimension(2,3,4); 
  13. } 

Explanation-

If you're only using the standard library? Don't worry about it, just use whatever you want. Most standard iterators are super cheap and extremely easy for the optimizer to understand, so it won't make a lick of difference.

Personally, I'm in the habit of using ++i, because I work with some heavy/large iterators designed for stepping through topology. In their case, the extra copy expense of the postfix increment is meaningful (especially since the increment often cannot be inlined). Actually, we only implement prefix iteration for them, so i++ gives a compilation error. Because of that, I'm in

  • void main() { ... } is wrong. If you're declaring main this way, stop. (Unless your code is running in a freestanding environment, in which case it could theoretically be correct.)
  • main() { ... } is acceptable in C89; the return type, which is not specified, defaults to int. However, this is no longer allowed in C99. Therefore...
  • int main() { ... } is the best way to write main if you don't care about the program arguments. If you care about program arguments, you need to declare the argc and argv parameters too. You should always define main in this way. Omitting the return type offers no adva

After some quick reading, here's what I found

  • Generic lambdas - you can now write lambda parameters with an auto type and the compiler will figure things out.
  • Lambdas parameters move - you can std::move things into the lambda (before this only copy or reference were possible)
  • Constexpr - it can now have if, switch or for statements and local variables in a constexpr
  • Return type deduction - auto myFunction() { }
  • digit separators - you can now write 1'000'000 instead of 1000000
  • binary literals - 0b prefix
  • sized deallocation - you can now have a global overload to delete for all sized allocations (pr

The common ground of those types is that they all point to something.

A pointer is a mutable variable that holds the address of some data.

A reference is an immutable variable that holds the address of something.

Mutable here means that it is possible to change what you point to. A reference once initialized will always point to the same address while it is possible to change the value of the pointer variable itself. That does not change the ability to alter the thing pointed to or referenced from.

For that reason we have pointer arithmetic, which basically means I can add an offset to a pointer t

There are no pointers in Python.

A pointer has a value identifying a single point in the machine's logical memory (an address).

An array (in C and C++) is a single (i.e. continguous) block of memory containing 1 or more objects that are homogeneous in type (all exactly the same type).

NB: In Computer Science the term 'array' is used for a wider range of indexed containers. But in C and C++ we tend to be careful to only use them to refer to arrays as defined in the language specifications.

On the one hand they are totally different things. One is an address and the other a block of memory. It's the same as asking "What's

Because this way, iterators are backward compatible with pointers, and containers are with arrays. This means that you can have:

  1. int a[5] = {5, 1, 7, 9, 3}; 
  2. std::sort(&a[0], &a[5]); 
  3. for (int val : a) 
  4. DoSth(val); 

Just the way you'd do with std::deque or std::vector.

The term “null pointer” tells you that the contents of the pointer is NULL, effectively pointing to memory address 0. It tells you absolutely nothing about the data type of the pointer (i.e., the type of data the pointer points to).

The term “void pointer” tells you that the data type of the pointer is “pointer to void”, sometimes referred to as a generic pointer. This means that, no matter what th

No, an array is not a pointer. Repeat after me: An array is the sum of its parts. The sooner you learn this, the better.

An array is a sequence of objects arranged consecutively in memory, its elements. Each element is part of the array. The value of the array is the sequence of values of its elements. The size of the array is the sum of the sizes of its elements.

A pointer is the address of a byte of memory. A pointer type is a scalar type; so a pointer is single, indivisible object, with no parts. The value of the pointer is the address. The size of a pointer is the size needed to represent th

About · Careers · Privacy · Terms · Contact · Languages · Your Ad Choices · Press ·
© Quora, Inc. 2022